package bookez.model.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import bookez.model.businessobject.TripDailyRecord;
import bookez.model.businessobject.TripSchedule;
import bookez.model.dao.TripDailyRecordDao;
import bookez.model.exception.BusinessLogicLayerException;
import bookez.model.exception.DataAccessLayerException;
import bookez.model.service.GenericService;
import bookez.model.service.TripDailyRecordService;

public class TripDailyRecordServiceImpl extends GenericService<TripDailyRecordDao> implements TripDailyRecordService {

	@Override
	public List<TripDailyRecord> getAll() throws BusinessLogicLayerException {
		// TODO Auto-generated method stub
		List<TripDailyRecord> result = null;
		
		try {
			result = getDao().findAll();
			return result;
		}//end try
		
		catch ( DataAccessLayerException dalex ) {
			throw new BusinessLogicLayerException( dalex );
		}//end catch
	}//end method getAll

	@Override
	public List<TripDailyRecord> generateRecord(TripSchedule schedule)
			throws BusinessLogicLayerException {
		// TODO Auto-generated method stub

		Date startingDate = schedule.getStartingDate();
		Date endingDate = schedule.getEndingDate();
		
		Calendar startDate = Calendar.getInstance();
		Calendar endDate = Calendar.getInstance();
		
		startDate.setTime( startingDate );
		endDate.setTime( endingDate );
		
		List<TripDailyRecord> records = new ArrayList<TripDailyRecord>();
		long daysBetween = 0;
		
		while( startDate.before( endDate ) ) {
			// Increase one day
			startDate.add( Calendar.DAY_OF_MONTH, 1 );
			
			TripDailyRecord record = new TripDailyRecord();
			record.setSchedule( schedule );
			record.setDepartureDate( new Date( startDate.getTimeInMillis() ) );
			
			records.add( record );
			
			daysBetween++;
		}//end while
		
		return records;
	}//ned method generateRecord

}//end class TripDailyRecordServiceImpl
